/**
 * @file ebex_cfg.h
 *
 * @date Sep 19, 2011
 * @author seth
 *
 * @brief This file is part of EBEX State Estimator, created for the EBEX project
 *
 * This software is copyright (C) 2011 Columbia University
 *
 * EBEX State Estimator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * EBEX State Estimator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EBEX State Estimator; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef EBEX_CFG_H_
#define EBEX_CFG_H_
#include <ebex_lookup.h>

#define EBEX_CFG_LINEBUF_LEN 1024

typedef enum
{
	EBEX_CFG_ERR_NONE = 0,
	EBEX_CFG_ERR_INVCMD,
	EBEX_CFG_ERR_INVSECTION,
	EBEX_CFG_ERR_INVDATA,
	EBEX_CFG_ERR_BADFORMAT,
	EBEX_CFG_ERR_CANTOPEN,
	EBEX_CFG_ERR_CALLBACK,
	EBEX_CFG_ERR_ENOMEM
} ebex_cfg_err_t;

#define _ebex_cfg_var_types(x,_)	\
	_(x,unknown)			\
	_(x,none)				\
	_(x,string)				\
	_(x,long_long)			\
	_(x,long)				\
	_(x,int)				\
	_(x,short)				\
	_(x,sizet)				\
	_(x,boolean)			\
	_(x,bool_by_exist)		\
	/*_(x,filename)*/			\
	/*_(x,directory)*/			\
	_(x,size_long_long)		\
	_(x,size_long)			\
	_(x,size_int)			\
	_(x,size_short)			\
	/*_(x,time)*/				\
	/*_(x,date)*/				\
	_(x,section)			\
	_(x,sectionstart)		\
	_(x,sectionend)			\
	_(x,ip)					\
	_(x,addr)				\
	_(x,hostname)			\
	_(x,float)				\
	_(x,double)

typedef enum
{
	EBEX_CFG_FLAGS_VAR,
	EBEX_CFG_FLAGS_CMDLINE,
	EBEX_CFG_FLAGS_ENVIRON,
	EBEX_CFG_FLAGS_SECTIONSTART,
	EBEX_CFG_FLAGS_SECTIONEND
} ebex_cfg_flags_t;


typedef int	(*ebex_cfg_callback_t)(const char *, const char *, const char *, const char *, ebex_cfg_flags_t, void *);

EBEX_PROCESS_FUNCTION_LOOKUP_TABLE(ebex_cfg_var_type, static);

typedef struct ebex_cfg_varhandler_s ebex_cfg_varhandler_t;

typedef struct ebex_cfg_varhandler_s
{
	ebex_cfg_varhandler_t *_next;
	char *var;
	char opt;
	e_ebex_cfg_var_type type;
	enum
	{
		EBEX_CFG_MODE_CALLBACK,
		EBEX_CFG_MODE_VAR
	} mode;
	int (*callback)();
	void *data;
	void *extra;
} ebex_cfg_varhandler_t;

int ebex_cfg_process_var(const char *var, const char *varargs, const char *value, ebex_cfg_flags_t flags);
int ebex_cfg_process(int argc, char **argv, const char *appname, const char *extra);
int ebex_cfg_register_var(const char *m_var, int m_type, void *m_data, char m_opt);
int ebex_cfg_register_handler(const char *m_var, char m_opt, int m_type, ebex_cfg_callback_t m_callback, void *m_extra);
void ebex_cfg_cleanup(void);

ebex_cfg_err_t ebex_cfg_geterrno(void);
void ebex_cfg_seterrstr(const char *usererrmsg);
char *ebex_cfg_geterrstr(void);
#endif /* EBEX_CFG_H_ */
